import React from 'react';
import { message } from 'antd';
import ProForm, { ProFormText } from '@ant-design/pro-form';
import getApiData from '@/services/cpasRequest';
import { intl } from '@/utils';
import { useModel } from 'umi';

const ChangePd: React.FC = () => {
  const [form] = ProForm.useForm();
  const { initialState } = useModel('@@initialState');
  // 密码验证
  // const checkPassword = (_: any, _value: string) => {
  //   const promise = Promise;
  //   // if (value && value !== form.getFieldValue('password')) {
  //   //   return promise.reject('两次密码输入不一致');// formatMessage({ id: 'userandregister.password.twice' })
  //   // }
  //   return promise.resolve();
  // };

  return (
    <div style={{ width: 330, margin: 'auto' }}>
      <ProForm
        onFinish={async (values) => {
          if (values.password !== values.password1) {
            message.error(intl('p.changePassword.error', '两次密码输入不一致，请重新输入。'));
            return;
          }
          const result = getApiData('resetpassword', {
            usercode: initialState?.curUser?.usercode,
            passwordOld: values.passwordOld,
            passwordnew: values.password,
          });
          if ((await result).code === 0) {
            message.success(intl('p.changePassword.success', '密码修改成功。请牢记您的新密码'));
            form.resetFields();
          } else {
            message.error((await result).data);
          }
        }}
        submitter={{
          searchConfig: {
            submitText: intl('p.changePassword.modify', '确定修改'),
          },
          render: (_, dom) => dom.pop(),
          submitButtonProps: {
            size: 'large',
            style: {
              width: '100%',
            },
          },
        }}
      >
        <h1 style={{ textAlign: 'center' }}>{intl('p.changePassword.title', '修改密码')}</h1>
        <ProFormText.Password
          fieldProps={{
            size: 'large',
          }}
          name="passwordOld"
          placeholder={intl('p.changePassword.enterOldPassword', '请输入旧密码!')}
          rules={[
            { required: true, message: intl('p.changePassword.enterOldPassword', '请输入旧密码!') },
          ]}
        />
        <ProFormText.Password
          fieldProps={{
            size: 'large',
          }}
          name="password"
          placeholder={intl('p.changePassword.enterOldPassword', '请输入新密码!')}
          rules={[
            { required: true, message: intl('p.changePassword.enterOldPassword', '请输入新密码!') },
            {
              min: 1,
              max: 18,
              message: intl('p.changePassword.passwordLength', '长度在 1 到 18 个字符'),
            },
          ]}
        />
        <ProFormText.Password
          fieldProps={{
            size: 'large',
          }}
          name="password1"
          placeholder={intl('p.changePassword.enterPasswordAgain', '请再次输入新密码!')}
          rules={[
            { required: true, message: intl('p.changePassword.enterOldPassword', '请输入新密码!') },
            // { validator: checkPassword, }
          ]}
        />
      </ProForm>
    </div>
  );
};

export default ChangePd;
